@echo off
REM ODrive固件编译CMD脚本

REM 设置工具链和make的路径
set MAKE_PATH=C:\Program Files (x86)\GnuWin32\bin
set TOOLCHAIN_PATH=D:\arm-gnu-toolchain-14.3.rel1-mingw-w64-i686-arm-none-eabi\bin

REM 检查工具是否存在
if not exist "%MAKE_PATH%\make.exe" (
    echo 错误: 未找到make工具，请检查路径是否正确: %MAKE_PATH%
    echo 建议从 http://gnuwin32.sourceforge.net/packages/make.htm 下载安装
    pause
    exit /b 1
)

if not exist "%TOOLCHAIN_PATH%\arm-none-eabi-gcc.exe" (
    echo 错误: 未找到ARM工具链，请检查路径是否正确: %TOOLCHAIN_PATH%
    pause
    exit /b 1
)

REM 添加到临时PATH
echo 设置临时环境变量...
set PATH=%TOOLCHAIN_PATH%;%MAKE_PATH%;%PATH%

REM 验证工具版本
echo 验证make版本: 
make --version

echo.
echo 验证ARM工具链版本: 
arm-none-eabi-gcc --version

echo.

REM 创建build目录（如果不存在）
if not exist "build" mkdir build

REM 执行编译
echo 开始编译ODrive固件...
echo 编译命令: make -j4
make -j4

REM 检查编译结果
if %errorlevel% equ 0 (
    echo.
    echo 编译成功!
    echo 输出文件位于build目录
    dir build\*.elf build\*.hex build\*.bin 2>nul
) else (
    echo.
    echo 编译失败，请查看上面的错误信息
)

pause